const request = require('./request.js')
const MS = require('./ms')
const {
    isString,
    isNumber,
    isBoolean,
    isArray,
    isUndefined
} = require('./dataType')

const seller = class seller {

    constructor() {
        this.header = {
            'content-type': 'application/json',
            'X-MS-ApplicationId': MS._config.applicationId,
            'X-MS-REST-API-KEY': MS._config.applicationKey,
            'X-MS-APP-ID': MS._config.appid
        }
    }

    query(options) {
        var params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.searchName) && isString(options.searchName)) {
            params.searchName = options.searchName
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.SEARCH_SELLER.url,
                MS._config.parameters.SHOP.SEARCH_SELLER.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(sellerId) {
        let params = {
            sellerId: sellerId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.REMOVE_SELLER.url,
                MS._config.parameters.SHOP.REMOVE_SELLER.method, params).then(res => {
                if (res.success) {
                    resolve(res.success)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let data = {
        }
        if(!isUndefined(options.id) && isString(options.id)) {
            data.id = options.id
        }
        if(!isUndefined(options.name) && isString(options.name)) {
            data.name = options.name
        }
        if(!isUndefined(options.logoPath) && isString(options.logoPath)) {
            data.logoPath = options.logoPath
        }
        if(!isUndefined(options.remark) && isString(options.remark)) {
            data.remark = options.remark
        }
        if(!isUndefined(options.phone) && isString(options.phone)) {
            data.phone = options.phone
        }
        if(!isUndefined(options.keeperName) && isString(options.keeperName)) {
            data.keeperName = options.keeperName
        }
        if(!isUndefined(options.address) && isString(options.address)) {
            data.address = options.address
        }
        if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
            data.longitude = options.longitude
        }
        if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
            data.latitude = options.latitude
        }
        if(!isUndefined(options.images) && isArray(options.images)) {
            data.images = options.images
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.SAVE_SELLER.url,
                MS._config.parameters.SHOP.SAVE_SELLER.method, {}, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findSellers() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_SELLER.url,
                MS._config.parameters.SHOP.FIND_SELLER.method).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(sellerId) {
        let params = {
            sellerId: sellerId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.GET_SELLER.url,
                MS._config.parameters.SHOP.GET_SELLER.method, params).then(
                res => {
                    if (res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
                }
            ).catch(err => {
                reject(err)
            })
        })
    }

    findSellerBySubCategoryId(subCategoryId) {
        let params = {
            subCategoryId: subCategoryId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_SELLER_BY_SUB_CATEGORY_ID.url,
                MS._config.parameters.SHOP.FIND_SELLER_BY_SUB_CATEGORY_ID.method, params).then(
                res => {
                    if (res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
                }
            ).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = seller
